/*
 * 常规活动设置
 * 分享红包活动
 * @date 2016/5/9
 */


var mtid = '';//红包活动id
var state = '0';//红包活动状态,默认关闭
//var passmtid = '';//乘客端邀请注册活动id
//var passstate = '0';//乘客端邀请注册活动状态,默认关闭

// 标题与内容tab切换
$(document).delegate('.activity-tabs-tit li', "click", function () {
//    $(this).addClass('on').siblings().removeClass('on');
//    $('.settings-tabs-con').eq($(this).index()).addClass('on').siblings().removeClass('on');
    switch ($(this).index()) {
        case 0://分享红包
            fillShareInfo();
            $(this).addClass('on').siblings().removeClass('on');
            $('.settings-tabs-con').eq(0).addClass('on').siblings().removeClass('on');
            break;
        case 1://乘客端邀请注册
            fillPassShareInfo();
            $(this).addClass('on').siblings().removeClass('on');
            $('.settings-tabs-con').eq(1).addClass('on').siblings().removeClass('on');
            break;
        case 2://司机端邀请注册
            break;
    }
});

// 页面初始化
$(function () {
    fillShareInfo();
});

//清空红包活动详情
function clearShareInfo() {
    $("#actpaymode").val('');
    $("#cnt").val('');
    $("#sum").val('');
    $("#business input[type='checkbox']").prop("checked", false);
    $("#whenexpire").val('');
    $("#expirein").val('');
    $("#expiretime").val('');
    $("#submitmark").text('');
}

//填充红包活动详情
function fillShareInfo() {
    if (mtid) {
        clearShareInfo();
    }
    $.ajax({
        type: "POST",
        url: "/activitysettings/gshareinfo",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({"type": "2"}),
        success: function (data) {
            if (data.status == 0) {
                if (data.mt) {
                    mtid = data.mt.id;
                    state = data.state;
                    _fillShareInfo(data.mt);
                } else {
                    $("#payall").attr("checked", true);
                    $("#close").attr("checked", true);
                }
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            bbx_alert_error("出错啦~");
        }
    });
}
function _fillShareInfo(mt) {
    $("#actpaymode option[value='" + mt.order_paymode + "']").prop("selected", true);//活动支付方式
    $("#cnt").val(mt.totalcount);//优惠券数量
    $("#sum").val(mt.totalsum);//总金额
    var busi = Number(mt.usabletype); //业务
    if (busi == -1) {
        $("#business input[type='checkbox']").prop("checked", true);
    } else {
        var busiArray = new Array(0, 0, 0, 0);
        for (var i = 3; i >= 0; i--) {
            var j = parseInt(busi / (Math.pow(2, i)));
            if (j > 0) {
                busiArray[i] = 1;
            }
            busi %= (Math.pow(2, i));
            if (busi == 0) {
                break;
            }
        }
        if (busiArray[0]) {
            $("#citypin").prop("checked", true);
        }
        if (busiArray[1]) {
            $("#citybao").prop("checked", true);
        }
        if (busiArray[2]) {
            $("#package").prop("checked", true);
        }
        if (busiArray[3]) {
            $("#city").prop("checked", true);
        }
    }
    switch (mt.paymode) {//优惠券支付方式
        case "1":
            $("#payonline").prop("checked", true);
            $("#payall").prop("checked", false);
            break;
        case "2":
            break;
        case "3":
            $("#payall").prop("checked", true);
            $("#payonline").prop("checked", false);
            break;
    }
    switch (mt.whenexpire) {//优惠券有效期
        case "0":
            $("#whenexpire option[value='0']").prop("selected", true);
            break;
        case "1":
            $("#whenexpire option[value='1']").prop("selected", true);
            $("#expirein").val(mt.expirein);
            $("#expireincss").show();
            break;
        case "2":
            $("#whenexpire option[value='2']").prop("selected", true);
            $("#expiretime").val(mt.expiretime);
            $("#expiretimecss").show();
            break;
    }
    if (state == '1') {
        $("#open").prop("checked", true);
        $("#close").prop("checked", false);
    } else {
        $("#close").prop("checked", true);
        $("#open").prop("checked", false);
    }
}

//点击红包活动提交
$(document).delegate("#submit", "click", function () {
    $("#submitmark").text('');
    if (!checkShareParam()) {
        layer.msg('请重新填写选项!', {icon: 2, time: 1000/*1秒后自动关闭*/});
        return;
    }
    if (mtid) {
        modify();
    } else {
        add();
    }
});

//检查红包活动参数
function checkShareParam() {
    var rightParam = true;
    $("#cnt").val() ? $("#cntmark").text('') : ($("#cntmark").text('请填写！'), rightParam = false);
    $("#sum").val() ? $("#summark").text('') : ($("#summark").text('请填写！'), rightParam = false);
    var allBusinessNotChecked = true;
    $("#business input").each(function () {
        if ($(this).prop("checked")) {
            allBusinessNotChecked = false;
        }
    });
    allBusinessNotChecked ? ($("#businessmark").text('请选择业务类型！'), rightParam = false) : $("#businessmark").text('');
    switch ($("#whenexpire option:selected").val()) {
        case "1":
            $("#expirein").val() ? $("#expireinmark").text('') : ($("#expireinmark").text('请填写！'), rightParam = false);
            break;
        case "2":
            $("#expiretime").val() ? $("#expiretimemark").text('') : ($("#expiretimemark").text('请填写！'), rightParam = false);
            break;
    }
    return rightParam;
}

//创建红包活动参数
function createShareParam() {
    var business = 0;
    if ($("#busAll").prop("checked")) {
        business = -1;
    } else {
        $("#business input:checked").each(function () {
            business += parseInt($(this).attr('val'));
        });
    }
    var setState = 0;//是否设置状态  0:不设置；1：设置
    if (state != $("#state input:checked").attr('val')) {
        setState = 1;
    }
    var param = JSON.stringify({
        'mtid': mtid,
        'acttype': "2",
        'actdescription': "分享红包活动",
        'couponname': '完成订单分享红包',
        'coupondescript': '完成订单后分享给好友',
        'sumstrategy': 6,
        'sum': 0,
        'totalsum': Number($("#sum").val()),
        'totalcount': Number($("#cnt").val()),
        'usabletype': business,
        'whenexpire': Number($("#whenexpire option:selected").val()),
        'expirein': Number($("#expirein").val()),
        'expiretime': $("#expiretime").val(),
        'order_paymode': Number($("#actpaymode option:selected").val()),
        'paymode': Number($("#coupaymode input:checked").attr('val')),
        'getmethod': 1,
        'maxcount': 5,
        'linkexpire': 0,
        'linkexpire_in': 0,
        'linkexpire_time': '',
        'selfget': 1,
        'uniquescope': 3,
        'setstate': setState,
        'state': Number($("#state input:checked").attr('val'))
    });
    console.log(param);
    return param;
}
//添加分享红包活动
function add() {
    var param = createShareParam();
    var layerIndex = layer.msg('新增中。。。', {icon: 16, time: 3600000, shade: [0.5, '#fff']});
    $.ajax({
        type: "POST",
        url: "/activitysettings/ashare",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            layer.close(layerIndex); //关闭加载层
            if (data.status == 0) {
                state = $("#state input:checked").attr('val');
                layer.msg('添加成功！', {icon: 1, time: 1000}, function () {
                    mtid = data.id;
                    state = $("#state input:checked").attr('val');
                    layer.closeAll();
                });
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            layer.close(layerIndex); //关闭加载层
            bbx_alert_error("出错啦~");
        }
    });
}

//修改分享红包活动
function modify() {
    var param = createShareParam();
    var layerIndex = layer.msg('修改中。。。', {icon: 16, time: 3600000, shade: [0.5, '#fff']});
    $.ajax({
        type: "POST",
        url: "/activitysettings/mshare",
        contentType: "application/json",
        dataType: "json",
        data: param,
        success: function (data) {
            layer.close(layerIndex); //关闭加载层
            if (data.status == 0) {
                state = $("#state input:checked").attr('val');
                $("#submitmark").text("√ 修改成功");
            } else if (data.status == -501) {//session过期
                window.location.href = '/timeout/index';
            } else {
                bbx_alert_error(data.message);
            }
        },
        error: function () {
            layer.close(layerIndex); //关闭加载层
            bbx_alert_error("出错啦~");
        }
    });
}
